package com.ydcycloud.variety.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.mvc.model.Pagination;
import com.smart.mvc.model.Result;
import com.smart.mvc.validator.annotation.ValidateParam;
import com.smart.util.StringUtils;
import com.ydcycloud.variety.model.SlideShow;
import com.ydcycloud.variety.service.SlideShowService;
import com.ydcycloud.sys.controller.BaseAdminController;

/**
 * 轮播图
 * */
@Controller
@RequestMapping("/admin/slideShow")
public class SlideShowController extends BaseAdminController {

	@Resource
	private SlideShowService slideShowService;

	@RequestMapping(method = RequestMethod.GET)
	public String execute() {
		return "/admin/ours/slideShowList";
	}

	/**
	 * 分页获取轮播图
	 * */
	@RequestMapping(value = "/list")
	public @ResponseBody
	Result list(Integer pageNo, Integer pageSize) {
		Pagination<Map<String, Object>> p = slideShowService.getSildeShow(pageNo,
				pageSize);
		return Result.createSuccessResult().setData(p);
	}

	/**
	 * 返回编辑企业列表页面
	 * 
	 * */
	@RequestMapping(method = RequestMethod.GET, value = "/edit")
	public String edit(Integer id, Model model) {
		SlideShow bean = new SlideShow();
		if (id != null) {
			bean = slideShowService.get(id);
		}
		model.addAttribute("bean", bean);
		return "/admin/ours/slideShowEdit";
	}

	/**
	 * 保存或修改轮播图
	 * */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody
	Result save(HttpSession httpSession, @ValidateParam(name="id")Integer id,
			@ValidateParam(name="图片路径") String imageUrl) {
		
		if (StringUtils.isBlank(imageUrl)) {
			return Result.createErrorResult().setMessage("轮播图不能为空");
		}
		SlideShow bean = new SlideShow();
		if (id != null) {
			bean = slideShowService.get(id);
			if (bean == null) {
				return Result.createErrorResult().setMessage("该图片不存在");
			}
		} else {
			bean.setCreateBy(getSessionAdmin(httpSession).getName());
			bean.setCreateTime(new Date());
		}
		bean.setImageUrl(imageUrl);;
		bean.setLastUpdateTime(new Date());
		slideShowService.save(bean);
		return Result.createSuccessResult().setData(bean);
	}

	/**
	 * 删除轮播图
	 * */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody
	Result delete(HttpServletRequest request, String ids) {
		if (StringUtils.isBlank(ids)) {
			return Result.createErrorResult().setMessage("请至少选择一项");
		} else {
			List<String> list = getAjaxIds(ids);
			ids = getIdsStr(list);
			slideShowService.deleteByIds(ids);
			return Result.createSuccessResult().setMessage("删除成功");
		}

	}

}
